package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService commendService;

    @PostMapping
    public JsonResult save(Travel travel,@RequestParam String content){
        JsonResult result = new JsonResult();
        try {
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.save(travel,content);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存失败");
        }
        return result;
    }

    @GetMapping("/{id}")
    public Travel getById(@PathVariable Long id){
        return travelService.getById(id);
    }

    @GetMapping
    public PageInfo<Travel> listAll(TravelQueryObject qo){
        qo.setState(Travel.STATE_RELEASE);
        return travelService.queryForList(qo);
    }

    @PutMapping
    public JsonResult update(Travel travel,@RequestParam String content){
        JsonResult result = new JsonResult();
        try {
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.update(travel,content);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存失败");
        }
        return result;
    }
    @GetMapping("/commends")
    public PageInfo<TravelCommend> list(TravelCommendQueryObject qo){
        return commendService.queryForApp(qo);
    }


    @GetMapping("/{userId}/getAmount")
    public int getAmountByUserId(@PathVariable Long userId){
        return travelService.getAmount(userId);
    }


}
